package com.design.graduation.asdemo.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.graphics.drawable.Drawable;


import com.design.graduation.asdemo.R;

import java.lang.reflect.Field;

/**
 * Created by liyugang on 6/29/15.
 */
public class ResourceUtils {

    public static int getColor(final Context context, final int resId) {
        return context.getResources().getColor(resId);
    }

    public static Drawable getDrawable(final Context context,
                                                final int resId) {
        return context.getResources().getDrawable(resId);
    }

    public static String getString(final Context context, final int resId) {
        return context.getResources().getString(resId);
    }

    public static Drawable getDrawable(final Context context,String name)
    {

        ApplicationInfo appInfo = context.getApplicationInfo();
        int resId = context.getResources().getIdentifier(name, "drawable",appInfo.packageName);
        return context.getResources().getDrawable(resId);

    }

    public static int getImageResource(String imgName) {
        int resId = 0;
        try {
            final Class<R.drawable> c = R.drawable.class;
            final Field field = c.getField(imgName);
            if (field != null) {
                resId = field.getInt(c);
            }
        } catch (final NoSuchFieldException ex) {
            ex.printStackTrace();
        } catch (final IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (final IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return resId;
    }

}
